<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/1/25
 * Time: 20:55
 */
class Place extends MY_Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('place/Place_model');
        $this->assign('index_channel','marketing');
    }

    function index(){
        $page = $this->input->get('page');
        $page = $page > 0? $page: 1;
        $conditions=array();
        $conditions['is_del'] = 0;
        if($this->input->get('name')){
            $conditions['name like'] = '%'.$this->input->get('name').'%';
            $this->assign('name', $this->input->get('name'));
        }else{
            $this->assign('name', '');
        }
        $base_url = url_query($conditions, '/m-admin/lecturer/index');

        $pagination = $this->Place_model->pagination($base_url, $page, $conditions, 'id desc');
        $this->session->set_userdata(
            'back_list_page',
            $this->get_self_url()
        );

        $this->assign('pagination', $pagination);
        $this->display();
    }

    function add(){
        $this->load->model('City_model');
        $condition = array('parent_code'=>'');
        $cities = $this->City_model->fetch_array($condition);
        $this->assign('province',$cities);
        $this->display();
    }

    function save(){
        if ($this->input->post('action') == 'save') {

            $id = $this->input->post('id')? $this->input->post('id'): 0;
            $data = array(
                'name' => trim($this->input->post('name')),
                'intro' => trim($this->input->post('intro')),
                'cover' => $this->input->post('cover'),
                'message' => trim($this->input->post('message')),
                'address' => trim($this->input->post('address')),
                'traffic' => json_encode(explode(',',trim($this->input->post('traffic')))),
                'area' => trim($this->input->post('area')),
                'is_audit' => 1,
                'provider' => trim($this->input->post('provider')),
            );

            if (!$id) {
                $data['created'] = date('Y-m-d',time());
                $result = $this->Place_model->save($data);
                $id = $result;
            } else {
                $result =  $this->Place_model->update($data,$id);
            }


            if ($id) {
                $data['id'] = $result;
                $this->json_response(true, $data);
            } else {
                $this->json_response(false);
            }
        }
    }

    function edit($place_id){
        if($place_id<1){
            exit('error by id');
        }
        $this->load->model('City_model');
        $condition = array('parent_code'=>'');
        $cities = $this->City_model->fetch_array($condition);
        $this->assign('province',$cities);
        $place = $this->Place_model->getOne(array('id'=>$place_id));
        if(!$place){
            exit('content is null');
        }
        $this->assign('place',$place);
        $this->display();
    }

    function del(){
        $place_id = intval(trim($this->input->post('id')));
        if($place_id<1){
            $this->json_response(false);
        }
        $place = $this->Place_model->soft_delete($place_id);
        if($place){
            $this->json_response(true);
        }
        $this->json_response(false);
    }
}